package sz.szsmk.citizencard.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.MycitycardActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryCitizenCardRes;
import sz.szsmk.citizencard.mvp.contract.IMycitycardContact;
import sz.szsmk.citizencard.mvp.presenter.MycitycardPresenter;
import sz.szsmk.citizencard.ui.adapter.MycitycardAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/26 17:38
 * description:我的市民卡
 */
@Route("MyCityCardActivity")
public class MyCityCardActivity extends IBaseActivity<MycitycardPresenter, MycitycardActivityBinding>
        implements IMycitycardContact.IMycitycardView, View.OnClickListener {

    ImageView back;
    TextView settingTitle;
    TextView mycitycardLine1;
    LinearLayout mycitycardBtn1;
    TextView mycitycardLine2;
    LinearLayout mycitycardBtn2;
    TextView mycitycardLine3;
    LinearLayout mycitycardBtn3;
    RecyclerView mycitycardRv;
    private List<QueryCitizenCardRes.CitizenCardInfoListBean> citizenCardInfoList = new ArrayList<>();
    private MycitycardAdapter mycitycardAdapter;
    private ArrayList<QueryCitizenCardRes.CitizenCardInfoListBean> mList1 = new ArrayList<>();
    private ArrayList<QueryCitizenCardRes.CitizenCardInfoListBean> mList2 =new ArrayList<>();
    private ArrayList<ArrayList<QueryCitizenCardRes.CitizenCardInfoListBean>> allList =new ArrayList<>();
    private RecyclerView mRv2;
    private String type = "0";

    @Override
    protected int attachLayoutRes() {
        return R.layout.mycitycard_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {



        back = findViewById(R.id.back);
        back.setOnClickListener(this);
        settingTitle = findViewById(R.id.setting_title);
        mycitycardLine1 = findViewById(R.id.mycitycard_line1);
        mycitycardBtn1= findViewById(R.id.mycitycard_btn1);
        mycitycardBtn1.setOnClickListener(this);
        mycitycardLine2= findViewById(R.id.mycitycard_line2);
        mycitycardBtn2= findViewById(R.id.mycitycard_btn2);
        mycitycardBtn2.setOnClickListener(this);
        mycitycardLine3= findViewById(R.id.mycitycard_line3);
        mycitycardBtn3= findViewById(R.id.mycitycard_btn3);
        mycitycardBtn3.setOnClickListener(this);
        mycitycardRv= findViewById(R.id.mycitycard_rv);





        showLoadingDialog();
        mPresenter.queryCitizenCard(this);

        settingTitle.setText("我的市民卡");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mycitycardRv.setLayoutManager(linearLayoutManager);

        mycitycardAdapter = new MycitycardAdapter(this);
        mycitycardRv.setAdapter(mycitycardAdapter);
        dataBinding.mycitycardMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("0".equals(type)){
                    if(allList.size()>0){
                        Bundle bundle = new Bundle();
                        bundle.putString("type",type);
                        Intent intent = new Intent(MyCityCardActivity.this,CitizenCardActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
                if("1".equals(type)){
                    if(mList1.size()>0){
                        Bundle bundle = new Bundle();
                        bundle.putString("type",type);
                        Intent intent = new Intent(MyCityCardActivity.this,CitizenCardActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }else{
                        Toast.makeText(MyCityCardActivity.this,"暂无卡片",Toast.LENGTH_LONG).show();
                    }
                }
                if("2".equals(type)){
                    if(mList2.size()>0){
                        Bundle bundle = new Bundle();
                        bundle.putString("type",type);
                        Intent intent = new Intent(MyCityCardActivity.this,CitizenCardActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }else{
                        Toast.makeText(MyCityCardActivity.this,"暂无卡片",Toast.LENGTH_LONG).show();
                    }
                }


            }
        });
        dataBinding.mycitycardLine1.setVisibility(View.VISIBLE);
        dataBinding.mycitycardLine2.setVisibility(View.GONE);
        dataBinding.mycitycardLine3.setVisibility(View.GONE);

    }

    @Override
    protected void updateViews() {

    }


    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.mycitycard_btn1) {
            dataBinding.mycitycardLine1.setVisibility(View.VISIBLE);
            dataBinding.mycitycardLine2.setVisibility(View.GONE);
            dataBinding.mycitycardLine3.setVisibility(View.GONE);

            allList.clear();
            allList.add(mList1);
            allList.add(mList2);
            mycitycardAdapter.setViewData(allList, 0);
            mycitycardAdapter.notifyDataSetChanged();
            type = "0";
        } else if (id == R.id.mycitycard_btn2) {
            type = "1";
            dataBinding.mycitycardLine1.setVisibility(View.GONE);
            dataBinding.mycitycardLine2.setVisibility(View.VISIBLE);
            dataBinding.mycitycardLine3.setVisibility(View.GONE);

            allList.clear();
            allList.add(mList1);
            mycitycardAdapter.setViewData(allList, 1);
            mycitycardAdapter.notifyDataSetChanged();
        } else if (id == R.id.mycitycard_btn3) {
            type = "2";
            dataBinding.mycitycardLine1.setVisibility(View.GONE);
            dataBinding.mycitycardLine2.setVisibility(View.GONE);
            dataBinding.mycitycardLine3.setVisibility(View.VISIBLE);

            allList.clear();
            allList.add(mList2);
            mycitycardAdapter.setViewData(allList, 2);
            mycitycardAdapter.notifyDataSetChanged();
        } else if (id == R.id.back) {
            finish();
        }
    }

    @Override
    public void queryCitizenCardFail() {
        dismissLoadingDialog();
    }

    @Override
    public void queryCitizenCardSucc(QueryCitizenCardRes response) {
        dismissLoadingDialog();
        citizenCardInfoList = response.getCitizenCardInfoList();
        mList1 = new ArrayList<>();
        mList2 = new ArrayList<>();
        for (int i = 0; i < citizenCardInfoList.size(); i++) {
            String tag = citizenCardInfoList.get(i).getTag();
            if (tag.equals("1")) {
                mList1.add(citizenCardInfoList.get(i));
            }
            if (tag.equals("2")) {
                mList2.add(citizenCardInfoList.get(i));
            }
        }

//        allList = new ArrayList<>();
        allList.add(mList1);
        allList.add(mList2);
        mycitycardAdapter.setViewData(allList,0);
        mycitycardAdapter.notifyDataSetChanged();

    }

    @Override
    public void onClick(View v) {
        onViewClicked(v);
    }
}
